package testUser;


import com.fang.bean.UserInfoBean;
import com.fang.dao.UserInfoBeanMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class testProvider {


    /**
     * 修改的测试
     */
    @Test
    public void test03() {
        String resource = "mybatis-config.xml";
        try{InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            try (SqlSession session = sqlSessionFactory.openSession()) {
                UserInfoBeanMapper userInfoBeanMapper = session.getMapper(UserInfoBeanMapper.class);
                UserInfoBean userI = new UserInfoBean();
                userI.setUserid(1);//进行修改操作必须进行设定主键
                userI.setUseraddress("淄博");
                userI.setRegdate(new Date());
                int i = userInfoBeanMapper.updateByPrimaryKeySelective(userI);
                session.commit();
                System.out.println("查询的结果集");
                System.out.println(i);
            }
        }catch (Exception e){
            System.out.println("   "+e.getMessage());
        }
    }


    /**
     * 新增的测试
     */
    @Test
    public void test02() {
        String resource = "mybatis-config.xml";
        try{InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            try (SqlSession session = sqlSessionFactory.openSession()) {
                UserInfoBeanMapper userInfoBeanMapper = session.getMapper(UserInfoBeanMapper.class);
                UserInfoBean userI = new UserInfoBean();
                userI.setUsername("user310");
                userI.setUserpsw("user310");
                userI.setUseraddress("淄博");
                userI.setRegdate(new Date());
                int i = userInfoBeanMapper.insertSelective(userI);
                session.commit();
                System.out.println("查询的结果集");
                System.out.println(i);
            }
        }catch (Exception e){
            System.out.println("   "+e.getMessage());
        }
    }
    /**
     * 查询的测试
     */
    @Test
    public void test01() {
        String resource = "mybatis-config.xml";
        try{InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            try (SqlSession session = sqlSessionFactory.openSession()) {
                UserInfoBeanMapper userInfoBeanMapper = session.getMapper(UserInfoBeanMapper.class);
                UserInfoBean userB = userInfoBeanMapper.selectByPrimaryKey(2);
                System.out.println("查询的结果集");
                System.out.println(userB);
            }
        }catch (Exception e){
            System.out.println("   "+e.getMessage());
        }
    }
}
